package com.jdd.dao.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.ResultGenerator;
import com.jdd.dao.entity.TFloor;
import com.jdd.dao.entity.THouse;
import com.jdd.dao.service.ITFloorService;
import com.jdd.dao.service.ITHouseService;
import com.jdd.dao.vo.HouseVo;
import com.jdd.dao.vo.SpaceVo;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.people.model.TProjectTreeModel;
import com.jdd.modules.userstaff.entity.User;
import com.jdd.modules.userstaff.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2022-09-19
 */
@Slf4j
@RestController
@RequestMapping("/house")
public class THouseController {
    @Resource
    private ITHouseService houseService;

    @Resource
    private ITFloorService  floorService;

    @Resource
    private TDeviceService deviceService;
    @Resource
    private IUserService userService;

    @GetMapping("/getHouseList")
    public Result<List<THouse>> geTHouseList(@RequestParam("floorId")String floorId){

        LambdaQueryWrapper<THouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(THouse::getFloorId,floorId);
        List<THouse> list = houseService.list(wrapper);
        Result<List<THouse>> result = new Result<>();
        result.setResult(list);
        result.setSuccess(true);
        return result;
    }


    @AutoLog(value = "根据业主用户查询房屋")
    @ApiOperation(value = "根据业主用户查询房屋", notes = "根据业主用户查询房屋")
    @GetMapping(value = "/getHouseByOwnerUser")
    public Result<List<THouse>> getHouseByOwnerUser(@RequestParam String id) {
        Result<List<THouse>> result = new Result<>();
        List<User> userList=new ArrayList<>();
        try {
            LambdaQueryWrapper<THouse> houseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            houseLambdaQueryWrapper.eq(THouse::getHostId,id);
            List<THouse> list = houseService.list(houseLambdaQueryWrapper);
            result.setSuccess(true);
            result.setResult(list);
        }catch (Exception e){
            log.error("根据业主用户查询房屋",e);
            result.setSuccess(false);
            result.setMessage("查询错误");
            return result;
        }
        return result;
    }
    @PostMapping(value = "/add")
    public Result<?> addNew(@RequestBody SpaceVo space) {
        return houseService.addNew(space);
    }
    @PostMapping(value = "/addHouse")
    public Result<?> addNew(@RequestBody HouseVo houseVo) {
        LoginUser loginUser = LoginUserUtils.getLoginUser();
        TFloor floor = floorService.getById(houseVo.getFloorId());
        String parentCode = floor.getParentCode() + "F" +floor.getCode();
        LambdaQueryWrapper<THouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(THouse::getFloorId,houseVo.getFloorId());
        queryWrapper.eq(THouse::getHouseName,houseVo.getHouseName());
        List<THouse> list=houseService.list(queryWrapper);
        if (list.size()>0){
            return ResultGenerator.genFailResult("存在相同房间名");
        }
        THouse house = new THouse();
        house.setHouseName(houseVo.getHouseName());
        house.setFloorId(houseVo.getFloorId());
        house.setCreateUser(loginUser.getId());
        house.setParentCode(parentCode);
        house.setLocateName(houseVo.getLocateName());
        house.setProjectId(houseVo.getProjectId());
        houseService.save(house);
        return ResultGenerator.genSuccessResult("添加成功");
    }
    @DeleteMapping(value = "/delHouse")
    public Result<?> delete(@RequestParam(name="id",required = true)String id){
        int count1 = deviceService.count(new QueryWrapper<TDevice>().eq("house_id", id));
        if (count1 > 0) {
            return ResultGenerator.genFailResult("该区域已绑定设备");
        }
        if (id==null ||"".equals(id)){
            return Result.error("删除失败");

        }else {
            houseService.removeById(id);
            return Result.ok("删除成功");
        }


    }

    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required = true)String ids){
        Result<THouse> result = new Result<THouse>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.houseService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    @PostMapping("/changeName")
    public Result changeName(@RequestBody THouse tHouse) {
        THouse house = houseService.getById(tHouse.getId());
        LambdaQueryWrapper<THouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(THouse::getFloorId,house.getFloorId());
        queryWrapper.eq(THouse::getHouseName,tHouse.getName());
        int count = houseService.count(queryWrapper);
        if (count>0){
            return ResultGenerator.genFailResult("修改名称已经存在");
        }
        String deviceParentCode = house.getParentCode()+ "H" + house.getCode();
        deviceService.updateDeviceAlias(deviceParentCode,4,house.getParentCode(),tHouse.getName());
        tHouse.setHouseName(tHouse.getName());
        houseService.saveOrUpdate(tHouse);
        return ResultGenerator.genSuccessResult(tHouse);
    }
    /*传入houseid，返回该项目id下的设备*/
    @GetMapping("/getChildren")
    public Result<List<TProjectTreeModel>> getChildren(@RequestParam(name = "id") String id) {
        List<TProjectTreeModel> tProjectTreeModels = new ArrayList<>();
        LambdaQueryWrapper<TDevice> tAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tAreaLambdaQueryWrapper.eq(TDevice::getHouseId, id);
        List<TDevice> areaList = deviceService.list(tAreaLambdaQueryWrapper);
        for (TDevice device : areaList) {
            tProjectTreeModels.add(new TProjectTreeModel(device));
        }
        return ResultGenerator.genSuccessResult(tProjectTreeModels);
    }
}
